package com.ellricky.baselib.ui.student;

import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageButton;

import androidx.appcompat.widget.AppCompatEditText;

import com.andr.common.http.callback.BaseCallback;
import com.andr.common.ui.adapter.BaseRecyclerAdapter;
import com.andr.common.ui.mvp.BasePresenter;
import com.andr.common.util.EmptyUtil;
import com.andr.common.util.ToastUtil;
import com.ellricky.baselib.R;
import com.ellricky.baselib.adapter.student.StuManageAdapter;
import com.ellricky.baselib.entity.student.StudentListEntity;
import com.ellricky.baselib.http.callback.EListCallback;
import com.ellricky.baselib.ui.base.BaseRefreshActivity;
import com.ellricky.baselib.util.EAppUtil;

import java.util.List;

import static com.ellricky.baselib.http.Api.API_STUDENT_LIST;

public class StudentSearchActivity extends BaseRefreshActivity<Void, BasePresenter<Void>, StudentListEntity> {

    private AppCompatEditText etSearch;
    private ImageButton ibSearch;

    @Override
    protected void loadDataOnInit() {
        mTvTitle.setText("搜索");
        etSearch = findViewById(R.id.etSearch);
        ibSearch = findViewById(R.id.ib_search);
        ibSearch.setOnClickListener(this);
        super.loadDataOnInit();
    }

    @Override
    public int getContentLayout() {
        return R.layout.base_activity_stu_search;
    }

    @Override
    protected void initParamMap() {
        super.initParamMap();
        paramMap.put("name", etSearch.getText().toString().trim());
    }

    @Override
    protected BaseCallback getResponseCallback() {
        return new EListCallback<StudentListEntity>(this) {
            @Override
            protected void onSuccess(List<StudentListEntity> list, String s) {
                dataSource = list;
                rvView.onLoadFinish(dataSource, refresh);
            }

            @Override
            protected void onErrorCode(String code, String msg) {
                super.onErrorCode(code, msg);
                rvView.onRefreshFailed(refresh);
            }

            @Override
            public void onHttpError(String code, String msg) {
                super.onHttpError(code, msg);
                rvView.onRefreshFailed(refresh);
            }
        };
    }

    @Override
    protected BaseRecyclerAdapter<StudentListEntity> getRvAdapter() {
        return null;
//        return new StuManageAdapter(this);
    }

    @Override
    protected String getUrlPath() {
        return API_STUDENT_LIST;
    }

    @Override
    protected void onClickView(View v) {
        super.onClickView(v);
        if (v == ibSearch) {
            String name = etSearch.getText().toString().trim();
            if (EmptyUtil.isEmpty(name)) {
                ToastUtil.show(this, "请输入学生名称");
                return;
            }
            loadDataOnInit();
        }
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        super.onItemClick(adapterView, view, i, l);
        StudentListEntity entity = adapter.get(i);
        EAppUtil.jumpPageWithId(this, StudentDetailActivity.class, entity.getId());
    }

}
